package net.system.sys.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.core.base.struts.BaseAction;
import net.core.common.HttpUtil;
import net.system.sys.service.IConfigService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

@Scope("request")
@Namespace("/system/config")
@InterceptorRefs(@InterceptorRef("basicStack"))
@ParentPackage("struts-default")
@Results( { @Result(name = "error", location = "/login.jsp") })
public class SyscfgAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	IConfigService service;
	List<HashMap> list;
	private HashMap<String, String> hmRetrun= new HashMap<String, String>();// 返回的HashMap
	
	@Action(value = "editConfig", results = { @Result(name = "success", location = "/WEB-INF/pages/system/main/ConfigSet.jsp") })
	public String pageConfig() {
		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		list = service.getConfigByFroup(reqMap);
		Iterator iter = reqMap.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			Object key = entry.getKey();
			Object val = entry.getValue();
			hmRetrun.put("in_"+key, (String) val);
		}
		return SUCCESS;
	}
	
	@Action(value = "saveConfig", results = { @Result(name = "success", location = "/WEB-INF/pages/system/main/ConfigSet.jsp") })
	public String saveConfig() {
		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		service.saveConfig(reqMap);
		list = service.getConfigByFroup(reqMap);
		Iterator iter = reqMap.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			Object key = entry.getKey();
			Object val = entry.getValue();
			hmRetrun.put("in_"+key, (String) val);
		}
		return SUCCESS;
	}
	
	@Action(value = "edtWWWCFG", results = { @Result(name = "success", location = "/WEB-INF/pages/system/main/WwwConfig.jsp") })
	public String edtWWWCFG() {
		list = service.getWWWCfg();
		System.out.println(list.toString());
		return SUCCESS;
	}
	
	@Action(value = "saveWWWCFG", results = { @Result(name = "success", location = "/WEB-INF/pages/system/main/WwwConfig.jsp") })
	public String saveWWWCFG() {
		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		service.saveWWWCFG(reqMap);
		list = service.getWWWCfg();
		return SUCCESS;
	}

	public List<HashMap> getList() {
		return list;
	}

	public void setList(List<HashMap> list) {
		this.list = list;
	}

	public HashMap<String, String> getHmRetrun() {
		return hmRetrun;
	}

	public void setHmRetrun(HashMap<String, String> hmRetrun) {
		this.hmRetrun = hmRetrun;
	}
	
}
